package edu.spec.xhd.pfers;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import edu.spec.xhd.R;

public class ActivityPrefers extends AppCompatActivity {

    private static final  String TAG = ActivityPrefers.class.getSimpleName();

    private EditText et_name;
    private EditText et_age;
    private EditText et_areyouok;
    private Button btn_submit;
    private Button btn_read;
    private Button btn_clear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prefers);
        initViews();
        initListeners();
    }

    private void initViews(){
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_areyouok = findViewById(R.id.et_areyouok);
        btn_submit = findViewById(R.id.btn_submit);
        btn_read = findViewById(R.id.btn_read);
        btn_clear = findViewById(R.id.btn_clear);
    }

    private void initListeners(){
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et_name.getText().toString();
                Integer age = Integer.valueOf(et_age.getText().toString());
                Boolean areyouok = Boolean.parseBoolean(et_areyouok.getText().toString());
                SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("name", name);
                editor.putInt("age", age);
                editor.putBoolean("areyouok", areyouok);
                editor.apply();
            }
        });
        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
                String name = sp.getString("name", "nobody");
                int age = sp.getInt("age", 0);
                boolean areyouok = sp.getBoolean("areyouok", false);
                Log.e(TAG, "name:" + name);
                Log.e(TAG, "age:" + age);
                Log.e(TAG, "areyouok:" + areyouok);
            }
        });
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.clear();
                editor.apply();
            }
        });

    }



}
